package com.bridge.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import com.bridge.baselibs.eventbus.EventBusUtil;
import com.bridge.baselibs.eventbus.EventMessage;
import com.bridge.common.dialog.VDialog;
import com.bridge.common.http.HttpUtils;
import com.bridge.common.interfaces.IHttpCallback;
import com.bridge.common.utils.ToastshowUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observable;
import okhttp3.ResponseBody;

public abstract class BaseFragment extends Fragment implements IHttpCallback, View.OnClickListener {
    private static final String TAG ="BaseFragment" ;
    public View view;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
    }

    @Override
    public void onDetach(){
        super.onDetach();
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup viewGroup = (ViewGroup) view.getParent();
            if (viewGroup != null)
                viewGroup.removeView(view);
        } else {
            view = inflater.inflate(layoutId(), container, false);
            initView(view);
        }
        return view;
    }

    /**
     * 发送请求
     * @param requestCode 请求码
     * @param call 请求体
     * @param
     * @param lodingStr 加载文字提示(传空串“”弹出无文字弹窗，传null不弹出进度条)
     * @param callback 回调
     */
    public void doHttp(int requestCode, Observable<ResponseBody> call, String lodingStr, IHttpCallback callback) {
        HttpUtils.getInstance().doHttp(requestCode, call,lodingStr,callback);
    }

    /**
     * 进度条加载回调，可统一加载
     * @param requestCode
     * @param text
     */
    @Override
    public void onLoading(int requestCode, String text) {
        if (null == text){

        }else {
            VDialog.getInstance().showLoadingDialog(text, getActivity(), true, true);
            //DialogUtils.getInstance().showProgressDialog(this,text,false);
        }

    }

    /**
     * 请求成功回调
     * @param requestCode 请求
     * @param result
     *
     */
    @Override
    public void onSuccess(int requestCode, String result) {
        //不论返回多少条数据，data都应以一个集合的方式返回
    }

    /**
     * 请求成功，但状态码错误回调，可做统一处理（登录过期，账号禁用等）s
     * @param requestCode 请求码
     * @param
     * @param msg 提示信息
     */
    @Override
    public void onFailure(int requestCode, String msg) {
        ToastshowUtils.showToastSafe(msg);
    }

    /**
     * 请求失败回调，可做统一操作（错误提示）
     * @param requestCode 请求码
     * @param responseCode 返回码
     */
    @Override
    public void onError(int requestCode, String responseCode) {
        ToastshowUtils.showToastSafe(responseCode);
    }

    /***********************************EventBus  start***********************************/

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(EventMessage event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(EventMessage event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(EventMessage event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(EventMessage event) {

    }
    /***********************************EventBus  end***********************************/
    /**
     * 获取ViewDataBing
     *
     * @param <T>
     * @return
     */
    public <T extends ViewDataBinding> T getBind() {
        return DataBindingUtil.bind(view);
    }
    protected abstract int layoutId();
    protected abstract void initView(View view);

    /**
     * 快速启动activity
     */
    public void turnToActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    public void turnToActivity(Class className, Bundle bundle) {
        Intent intent = new Intent(getActivity() ,className) ;
        intent.putExtras(bundle) ;
        startActivity(intent);
    }
    //隐式
    public void turnToActivity(Bundle bundle , String action) {
        Intent intent = new Intent() ;
        intent.putExtras(bundle) ;
        intent.setAction(action);
        startActivity(intent);
    }
    //隐式
    public void turnToActivity(String action) {
        Intent intent = new Intent() ;
        intent.setAction(action);
        startActivity(intent);
    }
}
